﻿using mds.BaseFrameModel;
using NVelocity;
using NVelocity.App;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace mds.CMSService
{
    public class CMSCore
    {
        private static CMSCore _instance = new CMSCore();

        public static CMSCore Instance
        {
            get { return CMSCore._instance; }

        }
        private VelocityEngine _vltEngine;
        private VelocityContext _vltContext;
        private CMSCore()
        {
            _vltEngine = new VelocityEngine();
            _vltEngine.Init();
            _vltContext = new VelocityContext();
        }
        public OperationResult<string> ParseOutput(Dictionary<string, string> contentData, String template)
        {
            StringWriter vltWriter = new StringWriter();
            if (contentData != null)
            {
                foreach (var kv in contentData)
                {
                    _vltContext.Put(kv.Key, kv.Value);
                }

                _vltEngine.Evaluate(_vltContext, vltWriter, null, template);
            }

            return new OperationResult<string>(true)
            {
                Data = vltWriter.GetStringBuilder().ToString()
            };
        }
    }
}
